/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductListView.java
 *
 * Created on 2011-feb-28
 */

package imat;

import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextField;
import se.chalmers.ait.dat215.project.CreditCard;
import se.chalmers.ait.dat215.project.Customer;

/**
 *
 * @author Gustav
 */
public class InfoView extends javax.swing.JPanel {

    private InfoPresenter infoPresenter;
    private Customer customer;
    private CreditCard creditCard;
    private java.util.ArrayList<JTextField> textFields = new java.util.ArrayList<JTextField>();

    /** Creates new form ProductListView */
    public InfoView() {
        initComponents();
        firstNameError.setVisible(false);
        lastNameError.setVisible(false);
        addressError.setVisible(false);
        postCodeError.setVisible(false);
        postAddressError.setVisible(false);
        phoneNumberError.setVisible(false);
        payError.setVisible(false);
        saveLabel.setVisible(false);

        customer = DataHandler.getInstance().getCustomer();
        creditCard = DataHandler.getInstance().getCreditCard();

        infoPresenter = new InfoPresenter(this);
        firstName.setText(customer.getFirstName());
        lastName.setText(customer.getLastName());
        address.setText(customer.getAddress());
        postCode.setText(customer.getPostCode());
        postAddress.setText(customer.getPostAddress());
        phoneNumber.setText(customer.getPhoneNumber());
        if (creditCard.getCardNumber().length() >= 15) {
            cardNumber1.setText(creditCard.getCardNumber().substring(0, 4));
            cardNumber2.setText(creditCard.getCardNumber().substring(4, 8));
            cardNumber3.setText(creditCard.getCardNumber().substring(8, 12));
            cardNumber4.setText(creditCard.getCardNumber().substring(12, 16));
        }
        validMonth.setText("" + creditCard.getValidMonth());
        validYear.setText("" + creditCard.getValidYear());
        verificationCode.setText("" + creditCard.getVerificationCode());
       jLabel13.setIcon(new ImageIcon("images/ccvnummer.png"));

       textFields.add(firstName);
       textFields.add(lastName);
       textFields.add(address);
       textFields.add(postCode);
       textFields.add(postAddress);
       textFields.add(phoneNumber);
       textFields.add(cardNumber1);
       textFields.add(cardNumber2);
       textFields.add(cardNumber3);
       textFields.add(cardNumber4);
       textFields.add(validMonth);
       textFields.add(validYear);
       textFields.add(verificationCode);
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        categoryTitle = new javax.swing.JLabel();
        productsPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        firstName = new javax.swing.JTextField();
        lastName = new javax.swing.JTextField();
        address = new javax.swing.JTextField();
        postCode = new javax.swing.JTextField();
        postAddress = new javax.swing.JTextField();
        phoneNumber = new javax.swing.JTextField();
        paymentPanel = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        cardNumber1 = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cardNumber2 = new javax.swing.JTextField();
        cardNumber3 = new javax.swing.JTextField();
        cardNumber4 = new javax.swing.JTextField();
        validYear = new javax.swing.JFormattedTextField();
        validMonth = new javax.swing.JFormattedTextField();
        verificationCode = new javax.swing.JFormattedTextField();
        jLabel14 = new javax.swing.JLabel();
        paymentMethod = new javax.swing.JComboBox();
        addressError = new javax.swing.JLabel();
        phoneNumberError = new javax.swing.JLabel();
        firstNameError = new javax.swing.JLabel();
        lastNameError = new javax.swing.JLabel();
        postCodeError = new javax.swing.JLabel();
        postAddressError = new javax.swing.JLabel();
        payError = new javax.swing.JLabel();
        saveLabel = new javax.swing.JLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(imat.IMatApp.class).getContext().getResourceMap(InfoView.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N

        categoryTitle.setFont(resourceMap.getFont("categoryTitle.font")); // NOI18N
        categoryTitle.setText(resourceMap.getString("categoryTitle.text")); // NOI18N
        categoryTitle.setName("categoryTitle"); // NOI18N

        productsPanel.setBackground(resourceMap.getColor("productsPanel.background")); // NOI18N
        productsPanel.setMinimumSize(new java.awt.Dimension(0, 44));
        productsPanel.setName("productsPanel"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        firstName.setText(resourceMap.getString("firstName.text")); // NOI18N
        firstName.setName("firstName"); // NOI18N
        firstName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InfoView.this.keyTyped(evt);
            }
        });

        lastName.setForeground(resourceMap.getColor("lastName.foreground")); // NOI18N
        lastName.setText(resourceMap.getString("lastName.text")); // NOI18N
        lastName.setName("lastName"); // NOI18N
        lastName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InfoView.this.keyTyped(evt);
            }
        });

        address.setText(resourceMap.getString("address.text")); // NOI18N
        address.setName("address"); // NOI18N
        address.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InfoView.this.keyTyped(evt);
            }
        });

        postCode.setText(resourceMap.getString("postCode.text")); // NOI18N
        postCode.setName("postCode"); // NOI18N
        postCode.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InfoView.this.keyTyped(evt);
            }
        });

        postAddress.setText(resourceMap.getString("postAddress.text")); // NOI18N
        postAddress.setName("postAddress"); // NOI18N
        postAddress.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InfoView.this.keyTyped(evt);
            }
        });

        phoneNumber.setText(resourceMap.getString("phoneNumber.text")); // NOI18N
        phoneNumber.setName("phoneNumber"); // NOI18N
        phoneNumber.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InfoView.this.keyTyped(evt);
            }
        });

        paymentPanel.setBackground(resourceMap.getColor("paymentPanel.background")); // NOI18N
        paymentPanel.setName("paymentPanel"); // NOI18N
        paymentPanel.setOpaque(false);
        paymentPanel.setLayout(new java.awt.CardLayout());

        jPanel4.setBackground(resourceMap.getColor("jPanel4.background")); // NOI18N
        jPanel4.setName("jPanel4"); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 608, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 183, Short.MAX_VALUE)
        );

        paymentPanel.add(jPanel4, "defaultCard");

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setOpaque(false);

        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        cardNumber1.setColumns(4);
        cardNumber1.setText(resourceMap.getString("cardNumber1.text")); // NOI18N
        cardNumber1.setName("cardNumber1"); // NOI18N
        cardNumber1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cardNumberKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InfoView.this.keyTyped(evt);
            }
        });

        jLabel13.setText(resourceMap.getString("jLabel13.text")); // NOI18N
        jLabel13.setName("jLabel13"); // NOI18N

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        cardNumber2.setText(resourceMap.getString("cardNumber2.text")); // NOI18N
        cardNumber2.setName("cardNumber2"); // NOI18N
        cardNumber2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cardNumberKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InfoView.this.keyTyped(evt);
            }
        });

        cardNumber3.setName("cardNumber3"); // NOI18N
        cardNumber3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cardNumberKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InfoView.this.keyTyped(evt);
            }
        });

        cardNumber4.setName("cardNumber4"); // NOI18N
        cardNumber4.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cardNumberKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InfoView.this.keyTyped(evt);
            }
        });

        validYear.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        validYear.setText(resourceMap.getString("validYear.text")); // NOI18N
        validYear.setName("validYear"); // NOI18N
        validYear.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InfoView.this.keyTyped(evt);
            }
        });

        validMonth.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        validMonth.setText(resourceMap.getString("validMonth.text")); // NOI18N
        validMonth.setName("validMonth"); // NOI18N
        validMonth.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InfoView.this.keyTyped(evt);
            }
        });

        verificationCode.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        verificationCode.setText(resourceMap.getString("verificationCode.text")); // NOI18N
        verificationCode.setName("verificationCode"); // NOI18N
        verificationCode.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InfoView.this.keyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8)
                    .addComponent(jLabel10))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(verificationCode)
                            .addComponent(validMonth, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(validYear, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))
                            .addComponent(jLabel13)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cardNumber1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cardNumber2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cardNumber3, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cardNumber4, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel12)
                .addGap(169, 169, 169))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(cardNumber1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cardNumber2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cardNumber3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cardNumber4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(validMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(validYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel10)
                    .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(verificationCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        paymentPanel.add(jPanel1, "creditCardCard");

        jLabel14.setText(resourceMap.getString("jLabel14.text")); // NOI18N
        jLabel14.setName("jLabel14"); // NOI18N

        paymentMethod.setBackground(resourceMap.getColor("paymentMethod.background")); // NOI18N
        paymentMethod.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Välj betalningsmetod", "Betalkort" }));
        paymentMethod.setName("paymentMethod"); // NOI18N
        paymentMethod.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                paymentMethodChanged(evt);
            }
        });

        addressError.setForeground(resourceMap.getColor("addressError.foreground")); // NOI18N
        addressError.setText(resourceMap.getString("addressError.text")); // NOI18N
        addressError.setName("addressError"); // NOI18N

        phoneNumberError.setForeground(resourceMap.getColor("phoneNumberError.foreground")); // NOI18N
        phoneNumberError.setText(resourceMap.getString("jLabel17.text")); // NOI18N
        phoneNumberError.setName("phoneNumberError"); // NOI18N

        firstNameError.setForeground(resourceMap.getColor("firstNameError.foreground")); // NOI18N
        firstNameError.setText(resourceMap.getString("firstNameError.text")); // NOI18N
        firstNameError.setName("firstNameError"); // NOI18N

        lastNameError.setForeground(resourceMap.getColor("lastNameError.foreground")); // NOI18N
        lastNameError.setText(resourceMap.getString("lastNameError.text")); // NOI18N
        lastNameError.setName("lastNameError"); // NOI18N

        postCodeError.setForeground(resourceMap.getColor("postCodeError.foreground")); // NOI18N
        postCodeError.setText(resourceMap.getString("postCodeError.text")); // NOI18N
        postCodeError.setName("postCodeError"); // NOI18N

        postAddressError.setForeground(resourceMap.getColor("postAddressError.foreground")); // NOI18N
        postAddressError.setText(resourceMap.getString("postAddressError.text")); // NOI18N
        postAddressError.setName("postAddressError"); // NOI18N

        payError.setForeground(resourceMap.getColor("payError.foreground")); // NOI18N
        payError.setText(resourceMap.getString("payError.text")); // NOI18N
        payError.setName("payError"); // NOI18N

        javax.swing.GroupLayout productsPanelLayout = new javax.swing.GroupLayout(productsPanel);
        productsPanel.setLayout(productsPanelLayout);
        productsPanelLayout.setHorizontalGroup(
            productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(productsPanelLayout.createSequentialGroup()
                .addGroup(productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(productsPanelLayout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel14)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(productsPanelLayout.createSequentialGroup()
                                .addComponent(paymentMethod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(payError))
                            .addGroup(productsPanelLayout.createSequentialGroup()
                                .addGroup(productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(firstName, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                                    .addComponent(lastName)
                                    .addComponent(address)
                                    .addComponent(postCode)
                                    .addComponent(postAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                                    .addComponent(phoneNumber))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(postAddressError)
                                    .addComponent(postCodeError)
                                    .addComponent(lastNameError)
                                    .addComponent(firstNameError)
                                    .addComponent(phoneNumberError)
                                    .addComponent(addressError)))))
                    .addGroup(productsPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(paymentPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        productsPanelLayout.setVerticalGroup(
            productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(productsPanelLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(firstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(firstNameError))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lastName)
                        .addComponent(lastNameError))
                    .addComponent(jLabel2))
                .addGap(40, 40, 40)
                .addGroup(productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(address, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(addressError))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(postCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(postCodeError))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(postAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(postAddressError))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(phoneNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(phoneNumberError))
                .addGap(28, 28, 28)
                .addGroup(productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(paymentMethod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(payError))
                .addGap(18, 18, 18)
                .addComponent(paymentPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        saveLabel.setFont(resourceMap.getFont("saveLabel.font")); // NOI18N
        saveLabel.setForeground(resourceMap.getColor("saveLabel.foreground")); // NOI18N
        saveLabel.setText(resourceMap.getString("saveLabel.text")); // NOI18N
        saveLabel.setName("saveLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(productsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(categoryTitle)
                        .addGap(30, 30, 30)
                        .addComponent(saveLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 351, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(categoryTitle)
                    .addComponent(saveLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(productsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void paymentMethodChanged(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_paymentMethodChanged
        int payMeth = paymentMethod.getSelectedIndex();
        infoPresenter.paymentMethodChanged(payMeth);
    }//GEN-LAST:event_paymentMethodChanged

    private void cardNumberKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cardNumberKeyPressed
       JTextField cardNumber = (JTextField)evt.getSource();
        String text = cardNumber.getText();

        if (text.length() > 4) {
            System.out.println("för lång");
            cardNumber.setText(text.substring(0, 4));
            return;
        }
        for (int i=0; i<text.length(); i++) {
            if (!Character.isDigit(text.charAt(i))) {
                System.out.println("inte siffra");
                return;
            }
        }

        int nr = 0;
        if (text.length() == 4) {
            for (int i=0; i<4; i++) {
                if (cardNumber == getCardNumberField(i)) {
                    System.out.println(i);
                    nr = i;
                    break;
                }
            }
            if (nr <= 2) {
                getCardNumberField(nr+1).requestFocus();
            }
        }
    }//GEN-LAST:event_cardNumberKeyPressed

    private void keyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_keyTyped
        JTextField t =(JTextField) evt.getSource();
        t.setForeground(Color.blue);
    }//GEN-LAST:event_keyTyped

    public JTextField getCardNumberField(int nr) {
        switch(nr) {
            case 0: return cardNumber1;
            case 1: return cardNumber2;
            case 2: return cardNumber3;
            case 3: return cardNumber4;
        }
        return null;
    }

    public JPanel getPaymentPanel() {
        return paymentPanel;
    }

    public void saveInfo() {
        infoPresenter.saveInfo();
    }

    public void clearColors() {
        for (JTextField t : textFields) {
            t.setForeground(Color.black);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField address;
    public javax.swing.JLabel addressError;
    private javax.swing.JTextField cardNumber1;
    private javax.swing.JTextField cardNumber2;
    private javax.swing.JTextField cardNumber3;
    private javax.swing.JTextField cardNumber4;
    private javax.swing.JLabel categoryTitle;
    private javax.swing.JTextField firstName;
    public javax.swing.JLabel firstNameError;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTextField lastName;
    public javax.swing.JLabel lastNameError;
    public javax.swing.JLabel payError;
    private javax.swing.JComboBox paymentMethod;
    private javax.swing.JPanel paymentPanel;
    private javax.swing.JTextField phoneNumber;
    public javax.swing.JLabel phoneNumberError;
    private javax.swing.JTextField postAddress;
    public javax.swing.JLabel postAddressError;
    private javax.swing.JTextField postCode;
    public javax.swing.JLabel postCodeError;
    private javax.swing.JPanel productsPanel;
    public javax.swing.JLabel saveLabel;
    private javax.swing.JFormattedTextField validMonth;
    private javax.swing.JFormattedTextField validYear;
    private javax.swing.JFormattedTextField verificationCode;
    // End of variables declaration//GEN-END:variables

    public String getFirstName() {
        return firstName.getText();
    }
    public String getLastName() {
        return lastName.getText();
    }
    public String getAddress() {
        return address.getText();
    }
    public String getPostCode() {
        return postCode.getText();
    }
    public String getPostAddress() {
        return postAddress.getText();
    }
    public String getPhoneNumber() {
        return phoneNumber.getText();
    }

    public String getCardNumber () {
        String cardNumber = cardNumber1.getText() + cardNumber2.getText()
                + cardNumber3.getText() + cardNumber4.getText();
        return cardNumber;
    }

    public int getVerificationCode() {
        if (!verificationCode.getText().equals(""))
            return Integer.parseInt(verificationCode.getText());
        else
            return 0;
    }

    public int getValidMonth() {
        if (!validMonth.getText().equals(""))
            return Integer.parseInt(validMonth.getText());
        else
            return 0;
    }

    public int getValidYear() {
        if (!validYear.getText().equals(""))
            return Integer.parseInt(validYear.getText());
        else
            return 0;
    }

    public void updateInfo() {
        saveLabel.setVisible(false);
        firstName.setText(customer.getFirstName());
        lastName.setText(customer.getLastName());
        address.setText(customer.getAddress());
        postCode.setText(customer.getPostCode());
        postAddress.setText(customer.getPostAddress());
        phoneNumber.setText(customer.getPhoneNumber());
        if (creditCard.getCardNumber().length() >= 15) {
            cardNumber1.setText(creditCard.getCardNumber().substring(0, 4));
            cardNumber2.setText(creditCard.getCardNumber().substring(4, 8));
            cardNumber3.setText(creditCard.getCardNumber().substring(8, 12));
            cardNumber4.setText(creditCard.getCardNumber().substring(12, 16));
        }
        if (creditCard.getValidMonth() != 0)
            validMonth.setText("" + creditCard.getValidMonth());
        if (creditCard.getValidYear() != 0)
            validYear.setText("" + creditCard.getValidYear());
        if (creditCard.getVerificationCode() != 0)
            verificationCode.setText("" + creditCard.getVerificationCode());
        clearColors();
    }

    public boolean isPayInfoComplete() {
        return infoPresenter.isPayInfoComplete();
    }

}
